/* Copyright (C) 2022 Beijing Ingenic Semiconductor CO.,LTD.
All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef MERT_AEC_H
#define MERT_AEC_H
#define MERT_FUNC_EXPORT __attribute__ ((visibility("default")))
#ifdef __cplusplus
extern "C"
{
#endif
    typedef void MertAecHandle;
    typedef struct eq_param
    {
        int enable;
        int band_num;
        float freq_low;
        float freq_high;
        float *band_value;
        int mode;
    } eq_param_t;
    typedef struct drc_param
    {
        int enable;
        float thd_low;
        float thd_high;
        float ratio;
    } drc_param_t;
    typedef struct agc_param
    {
        int enable;
        int mode;
        int gain_maxdb;
        int limi_enable;
        int target_dbfs;
    } agc_param_t;

    /**********************************************************************************
    Function:      // mert_aec_create
    Description:   // create echo cancellation handle
    Input:         // sample_rate: sample rate of input audio
                      dir_bin: dir of binfile.
                      memsize: allock memsize
    Return:        // MertAec Handle
    **********************************************************************************/
    MERT_FUNC_EXPORT MertAecHandle *mert_aec_create(int sample_rate,float suppression_ratio,char *dir_bin,int memsize);


    /**********************************************************************************
    Function:      // mert_aec_process
    Description:   // deal input mic and far audio, put no echo audio to  output
    Input:         // handle: MertAec Handle
                      input: ptr of mic and far for input frame
                      output: ptr of output frame, could be same as input ptr
                      size: num of frame sample point; size=memsize/sizeof(short);
    **********************************************************************************/
    MERT_FUNC_EXPORT void mert_aec_process(MertAecHandle *handle, short *input_mic,short *input_far, short *output, int size);

    /**********************************************************************************
    Function:      // mert_aec_free
    Description:   // free handle
    Input:         // handle: MertAec Handle
    **********************************************************************************/
    MERT_FUNC_EXPORT void mert_aec_free(MertAecHandle *handle);

    /**********************************************************************************
    Function:      // mert_aec_calib
    Description:   // 
    Input:         // handle: MertAec Handle
    **********************************************************************************/
    MERT_FUNC_EXPORT void mert_aec_harmjson(MertAecHandle *handle, char *harm_json);
    MERT_FUNC_EXPORT void mert_aec_harmration(MertAecHandle *handle,char *harm_json);
    MERT_FUNC_EXPORT void mert_aec_normjson(MertAecHandle *handle,char *norm_json);
    MERT_FUNC_EXPORT void mert_aec_normration(MertAecHandle *handle,char *norm_json);
    /**********************************************************************************
    Function:      // mert_aec_suspend
    Description:   // suspend echo cancellation, free some resource and Play mic audio.
    Input:         // handle: MertAec Handle
    **********************************************************************************/
    MERT_FUNC_EXPORT void mert_aec_suspend(MertAecHandle *handle);

    /**********************************************************************************
    Function:      // mert_aec_resume
    Description:   // resume echo cancellation.
    Input:         // handle: MertAec Handle
    **********************************************************************************/
    MERT_FUNC_EXPORT void mert_aec_resume(MertAecHandle *handle);

    /**********************************************************************************
    Function:      // mert_aec_eq_set
    Description:   // equalization attatched to aec,  do this in freq band, to avoid extra computation of fft ..
    Input:         // handle: MertAec Handle
                      eq_param: enable(0:disable, 1:enable), band_num(must be 128)
                                band_value(db: ratio=pow(10.0, band_value[i]/20.0))
    Default: disable
    **********************************************************************************/
    MERT_FUNC_EXPORT void mert_aec_eq_set(MertAecHandle *handle, eq_param_t eq_param);

    /**********************************************************************************
    Function:      // mert_aec_drc_set
    Description:   // dynamic-range-compression attatched to aec,  do this in freq band, to avoid extra computation of fft ..
    Input:         // handle: MertAec Handle
                      drc_param: enable(0:disable, 1:enable), thd_low(power = MAX(power - thd_low, 0))
                                thd_high and ratio(https://www.wikiaudio.org/dynamic-range-compression/)
                                           if (power>thd_high)power = ratio*(power-thd_high) + thd_high)
    Default: disable  thd_low:500 thd_high:50000 ratio:0.7
    **********************************************************************************/
    MERT_FUNC_EXPORT void mert_aec_drc_set(MertAecHandle *handle, drc_param_t drc_param);

    MERT_FUNC_EXPORT void mert_aec_agc_set(MertAecHandle *handle, agc_param_t agc_param);

#ifdef __cplusplus
}
#endif
#endif

